<?php

namespace app\admin\model;

use function GuzzleHttp\Psr7\parse_query;
use think\Exception;
use think\Model;


class StoreShop extends Model
{


    // 表名
    protected $name = 'store_shop';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'status_text',
        'create_time_text',
        'update_time_text'
    ];


    public static function init()
    {
        self::event('before_write', function ($data) {
            if ($data->douyin_localurl) {

                $localurl = self::unshort($data->douyin_localurl);
                $url_array = parse_query(parse_url($localurl, PHP_URL_QUERY));
                if (isset($url_array['id'])) {
                    $data->poi_id = $url_array['id'];
                }
                else {
                    throw new Exception('填写的分享链接错误,不能获取到地址');
                }

            }
            return $data;
        });
    }

    public static function unshort($url)
    {

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/8.0 (compatible; MSIE 8.0; Windows 7');
        curl_setopt($curl, CURLOPT_HEADER, 1);
        curl_setopt($curl, CURLOPT_NOBODY, 0);
        curl_setopt($curl, CURLOPT_TIMEOUT, 15);
        curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        $data = curl_exec($curl);
        if (!empty($data)) {
            $message = curl_getinfo($curl);
            curl_close($curl);
            $status = $message['http_code'];
            if ($status == '301' || $status == '302') {
                $UrlHeader = (get_headers($url, 1));
                return $UrlHeader['Location'];
            }

        }

    }


    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1'), '2' => __('Status 2')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUpdateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setCreateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setUpdateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


}
